﻿using Equipment.Management.EquipmentInfo;
using MCMesServer.Model;
using MCMesServer.PublicLogInfo;
using MesDAL.DTO;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MCMesServer.WorkForms.Pages
{
    public partial class UseCtr_MachineState : UserControl
    {
        private MachineInfoDTO machineInfo = new MachineInfoDTO();
        public UseCtr_MachineState(MachineInfoDTO _machineInfo)
        {
            InitializeComponent();
            machineInfo = _machineInfo;
        }

        public void UpdateShower(List<MachineInfoDTO> list)
        {
            try
            {
                this.Invoke(new EventHandler(delegate
                {
                    foreach (var item in list)
                    {
                        if (item.machine_num.Equals(machineInfo.machine_num))
                        {
                            machineInfo = item;
                        }
                    }
                    ui_title.Text = machineInfo.machine_name;
                    lbl_supplier.Text = machineInfo.machine_supplier;
                    lbl_class.Text = machineInfo.machine_class;
                    bool IsConnected = false;
                    if (MainWindow.ServerLink.ContainsKey(machineInfo.machine_num))
                    {
                        if (MainWindow.ServerLink[machineInfo.machine_num] == IntPtr.Zero)
                        {
                            IsConnected = false;
                        }
                        else
                        {
                            IsConnected = true;
                        }

                        EquipmentShowState machineState = TransformStateFuncs.TransformString2State(machineInfo.machine_state);

                        switch (machineState)
                        {
                            case EquipmentShowState.Produce:
                                lbl_state.Text = "正在加工";
                                ui_title.Style = Sunny.UI.UIStyle.Green;
                                break;
                            case EquipmentShowState.Wait:
                                lbl_state.Text = "设备待机";
                                ui_title.Style = Sunny.UI.UIStyle.Blue;
                                break;
                            case EquipmentShowState.Stop:
                                lbl_state.Text = "设备停机";
                                ui_title.Style = Sunny.UI.UIStyle.Orange;
                                break;
                            case EquipmentShowState.Alarm:
                                lbl_state.Text = "设备报警";
                                ui_title.Style = Sunny.UI.UIStyle.Red;
                                break;
                            case EquipmentShowState.Undefine:
                                lbl_state.Text = "未定义状态";
                                ui_title.Style = Sunny.UI.UIStyle.LayuiRed;
                                break;
                            default:
                                lbl_state.Text = "未定义状态";
                                ui_title.Style = Sunny.UI.UIStyle.LayuiRed;
                                break;
                        }
                    }
                    else
                    {
                        IsConnected = false;
                    }

                    if (!IsConnected)
                    {
                        lbl_state.Text = "未连接";
                        ui_title.Style = Sunny.UI.UIStyle.Gray;
                    }
                }));
            }
            catch (Exception ex)
            {
                PublicLogInfoFunc.RecordManualErrorMessage("设备状态显示", $"{machineInfo.machine_num}##{ex.ToString()}");
                return;
            }
        }
    }
}
